﻿using Newtonsoft.Json;
using QWManage.Utils;
using System;
using System.Collections.Generic;

namespace QWManage.API.Users96067
{
    /// <summary>
    /// 获取成员ID列表 获取企业成员的userid与对应的部门ID列表，
    /// </summary>
    internal class UsersAPI
    {
        //https://developer.work.weixin.qq.com/document/path/96067
        //请求方式：POST（HTTPS）
        //请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token=ACCESS_TOKEN

        /* 请求示例
            {
        	    "cursor": "xxxxxxx",
        	    "limit": 10000
            }
        */
        public static List<string> Invoke(string token)
        {
            List<string> ids = new List<string>();
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token={token}";
            UsersAgrs usersAgrs = new UsersAgrs();
            string parameters = JsonConvert.SerializeObject(usersAgrs);
            string result = HttpHelper.Submit(url, parameters);
            //{"errcode":0,"errmsg":"created","tagid":16}
            Root root = JsonConvert.DeserializeObject<Root>(result);
            if (root == null || root.errcode != 0)
                throw new Exception("API获取成员ID列表出错:" + result);

            foreach (var user in root.dept_user)
            {
                if (!ids.Contains(user.userid))
                    ids.Add(user.userid);
            }
            return ids;
        }

        public static Dictionary<string, List<int>> GetUserDeparmentsDic(string token)
        {
            Dictionary<string, List<int>> dic = new Dictionary<string, List<int>>();
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token={token}";
            UsersAgrs usersAgrs = new UsersAgrs();
            string parameters = JsonConvert.SerializeObject(usersAgrs);
            string result = HttpHelper.Submit(url, parameters);
            //{"errcode":0,"errmsg":"created","tagid":16}
            Root root = JsonConvert.DeserializeObject<Root>(result);
            if (root == null || root.errcode != 0) throw new Exception("API获取成员ID列表出错");
            foreach (var user in root.dept_user)
            {
                string key = user.userid;
                if (!dic.ContainsKey(key)) dic.Add(key, new List<int>());
                dic[key].Add(user.department);
            }
            return dic;
        }
    }


    #region 调用参数
    public class UsersAgrs
    {
        public string cursor { set; get; } = null;
        public int limit { set; get; } = 10000;
    }
    #endregion

    #region JSON
    public class Dept_user
    {
        /// <summary>
        /// 
        /// </summary>
        public string userid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int department { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Dept_user> dept_user { get; set; }
    }
    #endregion
}
